#!/bin/sh
# udhcpc script edited by Tim Riker <Tim@Rikers.org>

eth0_UAL_PATH=/ual/eth0
eth1_UAL_PATH=/ual/eth1
eth2_UAL_PATH=/ual/eth2
eth3_UAL_PATH=/ual/eth3

dns1_UAL_PATH=/ual/dns1
dns2_UAL_PATH=/ual/dns2

RESOLV_CONF="/etc/resolv.conf"

[ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }

NETMASK=""
[ -n "$subnet" ] && NETMASK="netmask $subnet"
BROADCAST="broadcast +"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"

case "$1" in
	deconfig)
		echo "Setting IP address 0.0.0.0 on $interface"
		ifconfig $interface 0.0.0.0
		;;

	renew|bound)
		echo "Setting IP address $ip on $interface"
		ifconfig $interface $ip $NETMASK $BROADCAST

		if [ -n "$router" ] ; then
			echo "Deleting routers"
			while route del default gw 0.0.0.0 dev $interface ; do
				:
			done

			metric=0
			for i in $router ; do
				echo "Adding router $i"
				route add default gw $i dev $interface metric $metric
				: $(( metric += 1 ))
			done
		fi

		echo "Recreating $RESOLV_CONF"
		# If the file is a symlink somewhere (like /etc/resolv.conf
		# pointing to /run/resolv.conf), make sure things work.
		realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
		tmpfile="$realconf-$$"
		> "$tmpfile"
		[ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
		for i in $dns ; do
			echo " Adding DNS server $i"
			echo "nameserver $i" >> "$tmpfile"
		done
		mv "$tmpfile" "$realconf"

		#weike: we need update eth0 status file
		if [ "x$interface" = "xeth0" ]; then
			tmp_eth0_error=0
			#Get current IPAddr
			tmp_eth0_ip=$(ifconfig eth0 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $2}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth0_ip} ]; then
				tmp_eth0_ip=0.0.0.0
				tmp_eth0_error=1
			fi

			#Get current NETMask
			tmp_eth0_mask=$(ifconfig eth0 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $4}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth0_mask} ]; then
				tmp_eth0_mask=0.0.0.0
				tmp_eth0_error=1
			fi

			#Get current GWAddr
			tmp_eth0_gw=$(ip route show |
			grep eth0 |
			grep default |
			awk '{print $3}')
			if [ -z ${tmp_eth0_gw} ]; then
				tmp_eth0_gw=0.0.0.0
				tmp_eth0_error=1
			fi

			#Get DNSAddr
			if [ -f /etc/resolv.conf ]; then
				tmp_eth0_dns1=$(cat /etc/resolv.conf | sed -n '1p'| awk '{print $2}')
				tmp_eth0_dns2=$(cat /etc/resolv.conf | sed -n '2p'| awk '{print $2}')
				if [ -z ${tmp_eth0_dns1} ]; then
					tmp_eth0_dns1=114.114.114.114
				fi
				if [ -z ${tmp_eth0_dns2} ]; then
					tmp_eth0_dns2=8.8.8.8
				fi
			else
				tmp_eth0_dns1=114.114.114.114
				tmp_eth0_dns2=8.8.8.8
			fi

			if [ -d ${eth0_UAL_PATH} ]; then
				echo  ${tmp_eth0_ip}    > ${eth0_UAL_PATH}/ipaddr
				echo  ${tmp_eth0_mask}  > ${eth0_UAL_PATH}/netmask
				echo  ${tmp_eth0_gw}    > ${eth0_UAL_PATH}/gateway
				echo  ${tmp_eth0_dns1}  > ${dns1_UAL_PATH}/ipaddr
				echo  ${tmp_eth0_dns2}  > ${dns2_UAL_PATH}/ipaddr
			fi
		elif [ "x$interface" = "xeth1" ]; then
			tmp_eth1_error=0
			#Get current IPAddr
			tmp_eth1_ip=$(ifconfig eth1 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $2}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth1_ip} ]; then
				tmp_eth1_ip=0.0.0.0
				tmp_eth1_error=1
			fi

			#Get current NETMask
			tmp_eth1_mask=$(ifconfig eth1 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $4}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth1_mask} ]; then
				tmp_eth1_mask=0.0.0.0
				tmp_eth1_error=1
			fi

			#Get current GWAddr
			tmp_eth1_gw=$(ip route show |
			grep eth1 |
			grep default |
			awk '{print $3}')
			if [ -z ${tmp_eth1_gw} ]; then
				tmp_eth1_gw=0.0.0.0
				tmp_eth1_error=1
			fi

			#Get DNSAddr
			if [ -f /etc/resolv.conf ]; then
				tmp_eth1_dns1=$(cat /etc/resolv.conf | sed -n '1p'| awk '{print $2}')
				tmp_eth1_dns2=$(cat /etc/resolv.conf | sed -n '2p'| awk '{print $2}')
				if [ -z ${tmp_eth1_dns1} ]; then
					tmp_eth1_dns1=114.114.114.114
				fi
				if [ -z ${tmp_eth1_dns2} ]; then
					tmp_eth1_dns2=8.8.8.8
				fi
			else
				tmp_eth1_dns1=114.114.114.114
				tmp_eth1_dns2=8.8.8.8
			fi

			if [ -d ${eth1_UAL_PATH} ]; then
				echo  ${tmp_eth1_ip}    > ${eth1_UAL_PATH}/ipaddr
				echo  ${tmp_eth1_mask}  > ${eth1_UAL_PATH}/netmask
				echo  ${tmp_eth1_gw}    > ${eth1_UAL_PATH}/gateway
				echo  ${tmp_eth1_dns1}  > ${dns1_UAL_PATH}/ipaddr
				echo  ${tmp_eth1_dns2}  > ${dns2_UAL_PATH}/ipaddr
			fi
		elif [ "x$interface" = "xeth2" ]; then
			tmp_eth2_error=0
			#Get current IPAddr
			tmp_eth2_ip=$(ifconfig eth2 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $2}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth2_ip} ]; then
				tmp_eth2_ip=0.0.0.0
				tmp_eth2_error=1
			fi

			#Get current NETMask
			tmp_eth2_mask=$(ifconfig eth2 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $4}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth2_mask} ]; then
				tmp_eth2_mask=0.0.0.0
				tmp_eth2_error=1
			fi

			#Get current GWAddr
			tmp_eth2_gw=$(ip route show |
			grep eth2 |
			grep default |
			awk '{print $3}')
			if [ -z ${tmp_eth2_gw} ]; then
				tmp_eth2_gw=0.0.0.0
				tmp_eth2_error=1
			fi

			#Get DNSAddr
			if [ -f /etc/resolv.conf ]; then
				tmp_eth2_dns1=$(cat /etc/resolv.conf | sed -n '1p'| awk '{print $2}')
				tmp_eth2_dns2=$(cat /etc/resolv.conf | sed -n '2p'| awk '{print $2}')
				if [ -z ${tmp_eth2_dns1} ]; then
					tmp_eth2_dns1=114.114.114.114
				fi
				if [ -z ${tmp_eth2_dns2} ]; then
					tmp_eth2_dns2=8.8.8.8
				fi
			else
				tmp_eth2_dns1=114.114.114.114
				tmp_eth2_dns2=8.8.8.8
			fi

			if [ -d ${eth2_UAL_PATH} ]; then
				echo  ${tmp_eth2_ip}    > ${eth2_UAL_PATH}/ipaddr
				echo  ${tmp_eth2_mask}  > ${eth2_UAL_PATH}/netmask
				echo  ${tmp_eth2_gw}    > ${eth2_UAL_PATH}/gateway
				echo  ${tmp_eth2_dns1}  > ${dns1_UAL_PATH}/ipaddr
				echo  ${tmp_eth2_dns2}  > ${dns2_UAL_PATH}/ipaddr
			fi
		elif [ "x$interface" = "xeth3" ]; then
			tmp_eth3_error=0
			#Get current IPAddr
			tmp_eth3_ip=$(ifconfig eth3 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $2}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth3_ip} ]; then
				tmp_eth3_ip=0.0.0.0
				tmp_eth3_error=1
			fi

			#Get current NETMask
			tmp_eth3_mask=$(ifconfig eth3 |
			grep "inet addr:" |
			grep -v "127.0.0.1" |
			awk '{print $4}' |
			cut -d: -f2 )
			if [ -z ${tmp_eth3_mask} ]; then
				tmp_eth3_mask=0.0.0.0
				tmp_eth3_error=1
			fi

			#Get current GWAddr
			tmp_eth3_gw=$(ip route show |
			grep eth3 |
			grep default |
			awk '{print $3}')
			if [ -z ${tmp_eth3_gw} ]; then
				tmp_eth3_gw=0.0.0.0
				tmp_eth3_error=1
			fi

			#Get DNSAddr
			if [ -f /etc/resolv.conf ]; then
				tmp_eth3_dns1=$(cat /etc/resolv.conf | sed -n '1p'| awk '{print $2}')
				tmp_eth3_dns2=$(cat /etc/resolv.conf | sed -n '2p'| awk '{print $2}')
				if [ -z ${tmp_eth3_dns1} ]; then
					tmp_eth3_dns1=114.114.114.114
				fi
				if [ -z ${tmp_eth3_dns2} ]; then
					tmp_eth3_dns2=8.8.8.8
				fi
			else
				tmp_eth3_dns1=114.114.114.114
				tmp_eth3_dns2=8.8.8.8
			fi

			if [ -d ${eth3_UAL_PATH} ]; then
				echo  ${tmp_eth3_ip}    > ${eth3_UAL_PATH}/ipaddr
				echo  ${tmp_eth3_mask}  > ${eth3_UAL_PATH}/netmask
				echo  ${tmp_eth3_gw}    > ${eth3_UAL_PATH}/gateway
				echo  ${tmp_eth3_dns1}  > ${dns1_UAL_PATH}/ipaddr
				echo  ${tmp_eth3_dns2}  > ${dns2_UAL_PATH}/ipaddr
			fi
		fi
		;;
esac

exit 0
